package com.no1.musicmgr.controller;


import com.no1.musicmgr.entity.User;
import com.no1.musicmgr.service.UserService;
import com.no1.musicmgr.util.UUIDUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Log4j
@RestController
@Tag(name = "UserImgController", description = "用户头像上传模块")
public class  UserImgController {
    @Autowired
    private UserService userService;

    public static String path = "C:/static/images";

    @Operation(summary = "用户头像上传")
    @PostMapping(value = "/user/yhtxUpload", consumes = "multipart/form-data")
    public String uploadFile(MultipartFile file) throws IOException {
        String fileName = null;

        //判断路径
        if (!file.isEmpty()) {
            // 获取用户头像文件名
            fileName = file.getOriginalFilename();
            // 获取用户头像文件存储路径
            Path uploadPath = Paths.get(path);
            // 如果路径不存在，则创建目录
            if (!Files.exists(uploadPath)) {
                try {
                    Files.createDirectories(uploadPath);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                file.transferTo(uploadPath.resolve(fileName).toFile());
            } catch (IOException e) {
                e.printStackTrace();
            }


        }



        return fileName;




    }
}
